package com.example.rjb_shoping.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.rjb_shoping.config.MsgResponse;
import com.example.rjb_shoping.entity.RjbAdmin;
import com.example.rjb_shoping.query.UserQuery;
import com.example.rjb_shoping.service.RjbAdminService;
import com.example.rjb_shoping.util.JsonUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * (RjbAdmin)表控制层
 *
 * @author makejava
 * @since 2021-06-09 20:42:21
 */
@RestController
@RequestMapping("rjbAdmin")
public class RjbAdminController {
    /**
     * 服务对象
     */
    @Resource
    private RjbAdminService rjbAdminService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public RjbAdmin selectOne(Integer id) {
        return this.rjbAdminService.queryById(id);
    }

    /**
     * 管理员登录 (通过表单提交数据)
     *
     * @return json数据
     */
    @PostMapping("login")
    public Map login(@RequestBody RjbAdmin rjbAdmin) {
        return rjbAdminService.login(rjbAdmin.getUsername(), rjbAdmin.getPassword());
    }

    /**
     * 管理注册
     *
     * @return
     */
    @PostMapping("registered")
    public Integer registered(@RequestBody String user) {
        // System.out.println(user);
        JSONObject jsonObject1 = JSONObject.parseObject(user);
        // String password = jsonObject1.getString("password");
        RjbAdmin ra = new RjbAdmin();
        ra.setUsername(jsonObject1.getString("name"));
        ra.setPassword(jsonObject1.getString("password"));
        ra.setANickname(jsonObject1.getString("nickName"));
        ra.setASex(jsonObject1.getString("sex"));
        ra.setAPhone(jsonObject1.getString("phone"));
        ra.setAEmai(jsonObject1.getString("email"));
        ra.setAImg(jsonObject1.getString("img"));
        ra.setADepartment("1");
        return rjbAdminService.register(ra);
//        return null;
    }

    /**
     * 分页查询
     *
     * @param userQuery
     * @return
     */
    @PostMapping("allList")
    public PageInfo<RjbAdmin> queryAllByLimit(@RequestBody UserQuery userQuery) {
        // System.out.println(userQuery);
        // JSONObject jsonObject = JSONObject.parseObject(offset);
        PageHelper.startPage(userQuery.getPageNum(), userQuery.getPageSize());
        final List<RjbAdmin> rjbAdmins = rjbAdminService.queryAll(userQuery);
        PageInfo<RjbAdmin> pageInfo = new PageInfo<>(rjbAdmins);
        return pageInfo;
    }

    /**
     * 修改用户信息
     *
     * @param rjbAdmin
     * @return
     */
    @PostMapping("updateOne")
    public RjbAdmin updateOne(@RequestBody RjbAdmin rjbAdmin) {
        return rjbAdminService.update(rjbAdmin);
    }

    /**
     * 删除用户
     *
     * @param id
     * @return
     */
    @GetMapping("deleteOne")
    public boolean deleteOne(Integer id) {
        return rjbAdminService.deleteByIdOne(id);
    }

    /**
     * 模糊查询
     *
     * @param username
     * @return
     */
    @GetMapping("likeAll")
    public List<RjbAdmin> likeAll(String username) {
        return rjbAdminService.likeAll(username);
    }

    /**
     * 根据用户名查询用户
     *
     * @param name
     * @return
     */
    @GetMapping("selectName")
    public boolean selectName(String name) {
        return rjbAdminService.selectName(name);
    }
}
